package com.qk.controller;


import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.service.BusinessService;
import com.qk.service.ClueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/businesses")
public class BusinessController {

    @Autowired
    private BusinessService businessService;
    @Autowired
    private ClueService clueService;

    @GetMapping
    public Result list(BusinessDto businessDto){
        log.info("查询业务：{}" , businessDto);
        PageResult<Business> pageResult = businessService.list(businessDto);
        return Result.success(pageResult);

    }
    @PostMapping
    public Result add(@RequestBody Business business){
        log.info("添加业务：{}" , business);
        business.setStatus(1);
        business.setChannel(1);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        businessService.save(business);
        return Result.success();
    }
    @PutMapping("/assign/{businessId}/{userId}")
    public Result assign(@PathVariable Integer businessId,@PathVariable Integer userId){
        log.info("分配商机：{}" , businessId);
        Business business = new Business();
        business.setId(businessId);
        business.setUserId(userId);
        business.setStatus(2);
        business.setUpdateTime(LocalDateTime.now());
        businessService.updateById(business);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据ID查询：" + id);
        Business business = businessService.getBusinessById(id);
        return Result.success(business);
    }

    @PutMapping
    public Result trackBusiness(@RequestBody Business business){
        log.info("修改商机：{}" , business);
        businessService.trackBusiness(business);
        return Result.success();
    }
     @PutMapping("/back/{id}")
    public Result back(@PathVariable Integer id){
        log.info("踢回公海：{}" , id);
        Business business = new Business();
        business.setId(id);
        business.setStatus(4);
        business.setUpdateTime(LocalDateTime.now());
        businessService.updateById(business);
        return Result.success();
    }

    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer  id){
        log.info("转客户：{}" , id);
        businessService.toCustomer(id);
        return Result.success();
    }

    @GetMapping("/pool")
    public Result pool(BusinessDto businessDto){
        log.info("公海列表");
        PageResult<Business> pageResult = businessService.listAll(businessDto);
        return Result.success(pageResult);

    }


}
